//
//  STRenewListViewModel.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/12/19.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "STRenewListViewModel.h"

#import "SpreadCityCellModel.h"

#import "SpreadCityInfoData.h"

@interface STRenewListViewModel()

@property (nonatomic, readwrite, strong) RACCommand* requestDataCmd;

@property (nonatomic, readwrite, strong) RACCommand* selectCmd;
@property (nonatomic, readwrite, strong) RACCommand* xfCmd;

@end

@implementation STRenewListViewModel

- (void)initialize
{
    [super initialize];
    
    @weakify(self);
    
    self.cellHeight = 54.0f;
    
    self.lastPage = 100;
    self.shouldMultiSections = NO;
    /// viewDidLoad后加载数据
    self.shouldRequestRemoteDataOnViewDidLoad = YES;
    /// 允许下拉刷新
    self.shouldPullDownToRefresh = NO;
    /// 允许上拉加载
    self.shouldPullUpToLoadMore = NO;
    
    self.xfCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input)
    {
        @strongify(self);
        
        SpreadCityListData* curData = input;
        
        NSMutableArray* areaIDs = [NSMutableArray array];
        [areaIDs addObject:curData.areaID];
        
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
        {
            [subscriber sendNext:areaIDs];
            [subscriber sendCompleted];
            
            return nil;
        }];
    }];
}

- (RACSignal *)requestRemoteDataSignalWithPage:(NSUInteger)page
{
    @weakify(self)
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        @strongify(self);
        
        UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
        
        NSDictionary* postData = interface_param_spread_spreadcity(um.userData.userID);
        
        @weakify(self);
        return [[AFNetWorkUtils racPOSTWthURL:url_interface_spread_spreadcity params:postData] subscribeNext:^(id x)
                {
                    @strongify(self)
                    
                    /// 转化数据
                    @weakify(self);
                    
                    SpreadCityInfoData* data = [SpreadCityInfoData modelWithJSON:x];
//                    data.count = 10;
//                    NSMutableArray* listDatas = [NSMutableArray array];
//                    for (NSInteger index=0; index<10; index++)
//                    {
//                        SpreadCityListData* listData = [[SpreadCityListData alloc] init];
//                        listData.areaName = HCStrFormat(@"汕头市-%d", (int)index);
//                        listData.areaID = @"245";
//                        listData.endTime = HCStrFormat(@"到期时间：%@", @"2018-01-01 00:00:00");
//                        [listDatas addObject:listData];
//                    }
//                    data.spreadArea = listDatas;
                    
                    self.cityNum = data.count;
                    
                    NSArray* datas = data.spreadArea;
                    
                    NSArray *viewModels = [datas.rac_sequence map:^id(id data)
                                           {
                                               @strongify(self);

                                               @weakify(self);
                                               SpreadCityCellModel *itemViewModel = [[SpreadCityCellModel alloc] initWithData:data cellHeight:self.cellHeight];
                                               [itemViewModel.xfCommand.executionSignals.switchToLatest subscribeNext:^(id x)
                                               {
                                                   @strongify(self);
                                                   
                                                   [self.xfCmd execute:x];
                                               }];
                                               
                                               return itemViewModel;
                                           }].array;
                    
                    self.dataSource = viewModels;
                    
                    [subscriber sendNext:nil];
                    [subscriber sendCompleted];
                }   error:^(NSError *error) {
                    //错误处理
                    
                    [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:error.userInfo[customErrorInfoKey]}]];
                    [subscriber sendCompleted];
                }
                ];
        
        return nil;
    }];
}

@end
